function status = GetNevada(filenaxls,PlateID,OutDir,AddName,Ext)
%GetNevada - download of one or more GNSS timeseries from Nevada database
%
%   StatStatus = GetNevada(fileStations,PlateID,OutDir,AddName,Ext)
%
% This function allows the timeseries download for the GNSS stations
% whose names are placed in the first column of the .xlsx/.xls file
% fileStations, from the second to the last row (the first row is the
% header) or on the only colums of the ASCII file fileStations. 
% If fileStations is undefined or empty, it can be interactively chosen.
%
% If PlateID is undefined, empty or is invalid (see below for the valid 
% PlateIDs), an interactive box allows the choice of the kind of time 
% series to be downloaded (IGS14 env, IGS14 XYZ, plates env).
% The valid Plate IDs are:
%       'IGS14 - XYZ','IGS14 - ENV',
%       'AF','AN','AR','AU','BU','CA','CO','EU','IN','MA','NA','NB','NZ',
%       'OK','ON','PA','PM','PS','SA','SB','SC','SL','SO','SU','WL'.
% The option valid PlateID is added in order to allow the use of this 
% function under GNU Octave. 
%
% The successfully downloaded files have name 
%   (OutDir)\(station name)(AddName)(Ext)   (Windows)
%   (OutDir)/(station name)(AddName)(Ext)   (Unix, MacOS)
%
% were:
%   - If OutDir is undefined or empty, the downloaded files are placed in
%     the current directory.
%   - If AddName is non-empty and its first character is not '.', it is
%     AddName = ['.' AddName].
%     If AddName is empty, the possible name component is taken from the
%     filename of Nevada database (for example, in the case of eurasian 
%     plate the default AddName is '.EU').
%   - If Ext is non-empty and Ext(1) is not '.', it is Ext = ['.' Ext].
%     If Ext is empty, the extension is taken from the filename of Nevada 
%     database (.tenv3 for env files and .txyz2 for XYZ files).
%
% The output cell variable StatStatus is such that StatStatus{k,1} is
% the name of the k-th GNSS station and StatStatus{k,2} is true if 
% the file download was successful and false elsewhere. 
 
% G. Teza, 2021, 2022.

if nargin < 5
    Ext = '';
elseif ~strcmp(Ext(1),'.')
    Ext = ['.' Ext];
end
if (nargin < 4) || isempty(AddName)
    AddName = '';
elseif ~strcmp(Ext(1),'.')
    AddName = ['.' AddName];
end
if (nargin < 3) || isempty(OutDir)
    OutDir = pwd;
else
    if exist(OutDir,'dir') ~= 7
        mkdir(OutDir);
    end
end
if nargin < 2 
    PlateID = [];
end
if ~isempty(PlateID)
    PlateCell = {'IGS14 - XYZ','IGS14 - ENV',...
        'AF','AN','AR','AU','BU','CA','CO','EU','IN','MA',...
        'NA','NB','NZ','OK','ON','PA','PM','PS','SA','SB',...
        'SC','SL','SO','SU','WL'};
    if ~ismember(PlateID,PlateCell)
        PlateID = [];
    end
end
if nargin < 1
    filenaxls = '';
end

b = readStationList(filenaxls);

nin = size(b,1);      

if isempty(PlateID)
    figIn = figure('Name','Plate choice');
    set(figIn,'Units','normalized','OuterPosition',[0 0 1 1]);
    imshow([])
    title('Choice of plate / file to be downloaded','FontSize',18,'Color','b');  
    axis equal;
    lsgen = {...
        'IGS14 - XYZ',...
        'IGS14 - ENV',...
        'AF - Africa - ENV',... 
        'AN - Antarctica - ENV',...
        'AR - Arabia - ENV',...
        'AU - Australia - ENV',...
        'BU - Burma - ENV',...
        'CA - Caribbean - ENV',...
        'CO - Cocos - ENV',...
        'EU - Eurasian - ENV',...
        'IN - Indian - ENV',...
        'MA - Mariana - ENV',...
        'NA - North America - ENV',...
        'NB - North Bismark - ENV',...
        'NZ - Nazca - ENV',...
        'OK - Okhotsk - ENV',...
        'ON - Okinawa - ENV',...
        'PA - Pacific - ENV',...
        'PM - Panama - ENV',...
        'PS - Philippine Sea - ENV',...
        'SA - South America - ENV',...
        'SB - South Bismark - ENV',...
        'SC - Scotia - ENV',...
        'SL - Shetland - ENV',...
        'SO - Somalia - ENV',...
        'SU - Sunda - ENV',...
        'WL - Woodlark - ENV',...
        'EXIT'};
    lbIn = uicontrol(figIn,'Style','listbox',...
        'string',lsgen,...
        'FontSize',14,...
        'Units','normalized','Position', [0.3 0.05 0.4 0.85],...
        'Callback', 'uiresume(gcbf)');
    uiwait(figIn);
    menuin = lbIn.Value;
    delete(lbIn);
    close(figIn);
    %
    if menuin == 28
        status = [];
        return
    end
else
    if strcmpi(PlateID,'IGS14 - XYZ')
        menuin = 1;
    elseif strcmpi(PlateID,'IGS14 - ENV')
        menuin = 2;
    else 
        menuin = [];    % menuin must be defined, but is unused if a valid PlateID > 2 is available
    end
end

ConstUrlString = 'http://geodesy.unr.edu/gps_timeseries/';
if menuin == 1
    ComplUrl = 'txyz/IGS14/';
    defAddName = '';
    defExt = '.txyz2';
elseif menuin == 2
    ComplUrl = 'tenv3/IGS14/';
    defAddName = '';
    defExt = '.tenv3';
else
    ComplUrlp = 'tenv3/plates/';
    if isempty(PlateID)
        if menuin == 3
            Namep = 'AF';
        elseif menuin == 4
            Namep = 'AN';
        elseif menuin == 5
            Namep = 'AR';
        elseif menuin == 6
            Namep = 'AU';
        elseif menuin == 7
            Namep = 'BU';
        elseif menuin == 8
            Namep = 'CA';
        elseif menuin == 9
            Namep = 'CO';
        elseif menuin == 10
            Namep = 'EU';
        elseif menuin == 11
            Namep = 'IN';
        elseif menuin == 12
            Namep = 'MA';
        elseif menuin == 13
            Namep = 'NA';
        elseif menuin == 14
            Namep = 'NB';
        elseif menuin == 15
            Namep = 'NZ';
        elseif menuin == 16
            Namep = 'OK';
        elseif menuin == 17
            Namep = 'ON';
        elseif menuin == 18
            Namep = 'PA';
        elseif menuin == 19
            Namep = 'PM';
        elseif menuin == 20
            Namep = 'PS';
        elseif menuin == 21
            Namep = 'SA';
        elseif menuin == 22
            Namep = 'SB';
        elseif menuin == 23
            Namep = 'SC';
        elseif menuin == 24
            Namep = 'SL';
        elseif menuin == 25
            Namep = 'SO';
        elseif menuin == 26
            Namep = 'SU';
        elseif menuin == 27
            Namep = 'WL';
        end
    else
        Namep = PlateID;
    end
    ComplUrl = [ComplUrlp Namep '/'];
    defAddName = ['.' Namep];
    defExt = '.tenv3';
end

ConstUrlCompl = [ConstUrlString ComplUrl];

if ~isempty(Ext)
    OutExt = Ext;
else
    OutExt = defExt;
end
if ~isempty(AddName)
    OutAddName = AddName;
else
    OutAddName = defAddName;
end

status = cell(nin,2);
for k = 1:nin
    namek = b{k,1};
    namek = strtrim(namek);     % to remove possible leading and trailing spaces
    namek = upper(namek);       % to warrant station name with capital letters
    status(k,1) = {namek}; 
    if isempty(namek)
        status(k,2) = {false};
        continue
    end
    UrlCompl = [ConstUrlCompl namek defAddName defExt];
    fprintf('Download of file %s in progress...\n',UrlCompl);
    filenOutk = fullfile(OutDir,[namek OutAddName OutExt]);
    try
        websave(filenOutk,UrlCompl);
        statusk = true;
        fprintf('Download of file %s completed\n',UrlCompl);
    catch
        statusk = false;
        fprintf('Unsuccessful download of file %s\n',UrlCompl);
    end
    status(k,2) = {statusk};
end